package com.wms.util;

import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.wms.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class CaptchaUtil {

    private LineCaptcha lineCaptcha = cn.hutool.captcha.CaptchaUtil.createLineCaptcha(200, 100);
    private CircleCaptcha circleCaptcha = cn.hutool.captcha.CaptchaUtil.createCircleCaptcha(200, 100);
    private ShearCaptcha shearCaptcha = cn.hutool.captcha.CaptchaUtil.createShearCaptcha(200, 100, 4, 4);

    @Autowired
    RedisService redisService;

    public String[] getLineImage(){
        lineCaptcha.createCode();
        String code = lineCaptcha.getCode();
        String key = UUID.randomUUID().toString();
        // 存储验证码
        redisService.setex(key,code,5*60);
        String imageBase = lineCaptcha.getImageBase64();
        String[] result = {key,imageBase};
        return result;
    }

    public String getCircleImage(){
        circleCaptcha.createCode();
        String code = circleCaptcha.getCode();
        // 存储验证码
        String imageBase = circleCaptcha.getImageBase64();
        return imageBase;
    }

    public String getShearImage(){
        shearCaptcha.createCode();
        String code = shearCaptcha.getCode();
        // 存储验证码
        String imageBase = shearCaptcha.getImageBase64();
        return imageBase;
    }

//    public static void main(String[] args) {
//        System.out.println("线段干扰验证码：\n"+getLineImage());
//        System.out.println("圆形干扰验证码：\n"+getCircleImage());
//        System.out.println("扭曲干扰验证码：\n"+getShearImage());
//    }


}